package org.ben.security;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;

import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.security.crypto.keygen.KeyGenerators;

public class Cryptor {
    private TextEncryptor encryptor;

    Cryptor(String pwd, String salt) {
        if(null == encryptor)
            encryptor = Encryptors.text(pwd, salt);
    }

    private void encryptor(String inFile, String outFile) throws Exception {
        LinkedList<String> encText = new LinkedList<String>();
        BufferedReader brd = null;
        try {
            brd = new BufferedReader(new FileReader(new File(inFile)));
            String content;
            while ((content = brd.readLine()) != null)
                encText.offer(encryptor.encrypt(content));
        } catch (IOException e) {
            e.printStackTrace();
            throw new Exception("cannot read input file");
        } finally {
            if (null != brd)
                try {
                    brd.close();
                } catch (IOException e) {
                    throw new Exception("cannot close input file");
                }
        }

        BufferedWriter bwt = null;
        bwt = new BufferedWriter(new FileWriter(new File(outFile)));
        Iterator<String> itr = encText.iterator();
        while (itr.hasNext()) {
            bwt.write(itr.next());
            bwt.newLine();
        }
        encText.clear();
        if(null != bwt)
            bwt.close();
    }

    private void decryptor(String inFile, String outFile) throws Exception {
        LinkedList<String> decText = new LinkedList<String>();
        BufferedReader brd = null;
        try {
            brd = new BufferedReader(new FileReader(new File(inFile)));
            String content;
            while ((content = brd.readLine()) != null) {
                decText.offer(encryptor.decrypt(content));
            }
        } catch (IOException e) {
            throw new Exception("cannot read input file");
        } finally {
            if (null != brd)
                try {
                    brd.close();
                } catch (IOException e) {
                    throw new Exception("cannot close input file");
                }
        }

        //"src/main/java/org/ben/srcurity/decryptOut.txt"
        BufferedWriter bwt = new BufferedWriter(new FileWriter(new File(outFile)));
        Iterator<String> itr = decText.iterator();
        while (itr.hasNext()) {
            bwt.write(itr.next());
            bwt.newLine();
        }
        decText.clear();
        if(null != bwt)
            bwt.close();
    }

    public static void main(String[] args) throws Exception {
        System.out.println(args.length);
        if(args.length == 4) {
            String inFile = args[2].replace("\\", "/");
            String outFile = args[3].replace("\\", "/");
//            new Cryptor("secretKey","d15baf531ccc62c5").encryptor("E:/inFile.txt", "E:/outFile.txt");
            new Cryptor(args[0],args[1]).encryptor(inFile,outFile);
//            new Cryptor(args[0],args[1]).decryptor(inFile,outFile);
        }
//        System.out.println(KeyGenerators.string().generateKey());
    }
}
